package com.example.ch04_04;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private ViewGroup rootLayout;
	private TextView pick;
	private Spinner choose;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		this.registerForContextMenu(rootLayout);
	}

	private void initView() {
		// TODO Auto-generated method stub
		rootLayout = (ViewGroup) findViewById(R.id.action_settings);
		pick = (TextView) findViewById(R.id.textView1) ;
		choose = (Spinner) findViewById(R.id.spinner1);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
				R.array.colors, android.R.layout.simple_spinner_item);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	super.onCreateContextMenu(menu, v, menuInfo);
	menu.add(0, 0, 1, "Option 1");
	menu.add(0, 1, 2, "Option 2");
	menu.add(0, 2, 3, "Option 3");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
	Toast.makeText(this, item.getTitle().toString(),
	Toast.LENGTH_LONG).show();
	return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	menu.add(Menu.NONE, Menu.FIRST + 1, 5, "刪除").setIcon(android.R.drawable.ic_menu_delete);
	menu.add(Menu.NONE, Menu.FIRST + 2, 2, "儲存").setIcon(android.R.drawable.ic_menu_edit);
	menu.add(Menu.NONE, Menu.FIRST + 3, 6, "説明").setIcon(android.R.drawable.ic_menu_help);
	menu.add(Menu.NONE, Menu.FIRST + 4, 1, "新增").setIcon(android.R.drawable.ic_menu_add);
	menu.add(Menu.NONE, Menu.FIRST + 5, 4, "明細").setIcon(android.R.drawable.ic_menu_info_details);
	menu.add(Menu.NONE, Menu.FIRST + 6, 3, "傳送").setIcon(android.R.drawable.ic_menu_send);
	return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		String ItemText = item.getTitle().toString();
		Toast.makeText(this, ItemText, Toast.LENGTH_SHORT).show();
		return super.onMenuItemSelected(featureId, item);
	}

}
